package com.basic.library.view;


import android.content.Context;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatEditText;

import java.lang.reflect.Field;
import java.util.ArrayList;

/**
 * @author yj
 * @description
 */
public class MEditText extends AppCompatEditText {
    public MEditText(@NonNull Context context) {
        this(context, null);
    }

    public MEditText(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, android.R.attr.editTextStyle);
    }

    public MEditText(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * @param watcher 确保只有一个watcher
     */
    public void setTextChangedListener(TextWatcher watcher) {
        try {
            clearTextWatchers(this);
        } catch (Exception e) {
            e.printStackTrace();
        }
        addTextChangedListener(watcher);
    }

    /**
     * ① 一般情况下，双向绑定和手动添加 TextWatcher最好不要一起用
     * ② 一起用时，手动添加的TextWatcher最好只做取值操作(取值就交给双向绑定自带的watcher)，不做赋值的操作
     */
    public static void clearTextWatchers(TextView t) throws Exception {
        Class clz = t.getClass();
        boolean isTextView = TextUtils.equals(clz.getCanonicalName(), "android.widget.TextView");
        while (!isTextView) {
            clz = clz.getSuperclass();
            isTextView = (clz != null && TextUtils.equals(clz.getCanonicalName(), "android.widget.TextView"));
        }
        if (clz != null) {
            for (Field field : clz.getDeclaredFields()) {
                if (TextUtils.equals("mListeners", field.getName())) {
                    field.setAccessible(true);
                    ArrayList<TextWatcher> watchers = (ArrayList<TextWatcher>) field.get(t);
                    if (watchers != null) {
                        watchers.clear();
                    }
                    return;
                }
            }
        }
    }

    public static ArrayList<TextWatcher> getWatchers(TextView t) {
        try {
            Class clz = t.getClass();
            boolean isTextView = TextUtils.equals(clz.getCanonicalName(), "android.widget.TextView");
            while (!isTextView) {
                clz = clz.getSuperclass();
                isTextView = (clz != null && TextUtils.equals(clz.getCanonicalName(), "android.widget.TextView"));
            }
            if (clz != null) {
                for (Field field : clz.getDeclaredFields()) {
                    if (TextUtils.equals("mListeners", field.getName())) {
                        field.setAccessible(true);
                        ArrayList<TextWatcher> watchers = (ArrayList<TextWatcher>) field.get(t);
                        if (watchers != null) {
                            return watchers;
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ArrayList<>();
    }
}
